/**   
* @Title: TaskConfigRunImpl.java 
* @Package cn.com.gome.task.service.impl 
* @Description: 任务参数变更执行器业务实现 
* @author chenmin-ds   
* @date 2015年1月21日 上午10:29:17 
* @company cn.com.gome
* @version V1.0   
*/ 


package cn.com.klun.tjy.service.Impl;

import cn.com.klun.tjy.cache.SystemConfigInfoCache;
import cn.com.klun.tjy.common.factory.SpringBeanFactory;
import cn.com.klun.tjy.dao.TaskInfoDAO;
import cn.com.klun.tjy.dao.entity.TblTaskBasicConfig;
import cn.com.klun.tjy.service.TimerBuildService;
import cn.com.klun.util.BasisConstants;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/** 
 * @ClassName: TaskConfigRunImpl 
 * @Description: 任务参数变更执行器业务实现 
 */
public class TaskConfigRunImpl implements Job {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

	/*
	 * <p>Description: 任务参数变更执行器业务实现 </p>
	 */
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		Object obj = context.getJobDetail().getJobDataMap().get("taskInfoDAO");
		try{
			if(obj != null){
				TaskInfoDAO taskInfoDAO = (TaskInfoDAO) obj;
				List<TblTaskBasicConfig> tbcList = taskInfoDAO.getTaskBasicConfig();

				upTblTaskBasicConfig(tbcList);
				logger.info("任务参数变更执行成功。。。");
				
				//读取站点推送配置到缓存 TODO 作用
				// SystemConfigInfoCache.setSiteList(taskInfoDAO.getSiteChildTask());
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	* @Title: upTblTaskBasicConfig 
	* @Description: 执行任务配置信息变更 
	* @param tbcList    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	private void upTblTaskBasicConfig(List<TblTaskBasicConfig> tbcList){
		if(tbcList.size()>0){
			TimerBuildService timerBuildService = (TimerBuildService) SpringBeanFactory.getBean("childTimerBuildServiceImpl");
			for(TblTaskBasicConfig config : tbcList){
				String key = SystemConfigInfoCache.updateTaskTimeMap.get(config.getId()+"_"+ BasisConstants.CHILD_TASK_RUN);
				if(StringUtils.isEmpty(key)){
					timerBuildService.createTimer(config);
				}else{
					if(1 == config.getStatus()){
						if(!key.equals(config.getUpTime())){// 如果修改时间不相等则 证明有被修改过
							timerBuildService.updateTimer(config);
						}
					}else{
						timerBuildService.deleteTimer(config);
					}
				}
			}
		}
		SystemConfigInfoCache.ruleList = tbcList;
	}
}
